package com.huangjue.backend.controller;

import com.huangjue.backend.entity.VideoFinish;
import com.huangjue.backend.service.VideoFinishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/video-finish")
public class VideoFinishController {

    @Autowired
    private VideoFinishService videoFinishService;

    /**
     * 记录视频观看
     */
    @PostMapping("/mark-watched")
    public Map<String, Object> markVideoWatched(@RequestBody Map<String, Object> request) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            Integer userId = (Integer) request.get("userId");
            Integer demoId = (Integer) request.get("demoId");
            Integer videoId = (Integer) request.get("videoId");
            
            if (userId == null || demoId == null || videoId == null) {
                response.put("success", false);
                response.put("message", "参数不完整");
                return response;
            }
            
            boolean result = videoFinishService.markVideoWatched(userId, demoId, videoId);
            
            response.put("success", result);
            response.put("message", result ? "记录成功" : "记录失败");
            
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "服务器错误: " + e.getMessage());
        }
        
        return response;
    }

    /**
     * 检查视频是否已观看
     */
    @GetMapping("/check-watched")
    public Map<String, Object> checkVideoWatched(
            @RequestParam Integer userId,
            @RequestParam Integer demoId,
            @RequestParam Integer videoId) {
        
        Map<String, Object> response = new HashMap<>();
        
        try {
            boolean watched = videoFinishService.isVideoWatched(userId, demoId, videoId);
            
            response.put("success", true);
            response.put("watched", watched);
            response.put("message", watched ? "已观看" : "未观看");
            
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "服务器错误: " + e.getMessage());
        }
        
        return response;
    }

    /**
     * 获取用户在某个demo下的所有观看记录
     */
    @GetMapping("/watched-videos")
    public Map<String, Object> getWatchedVideos(
            @RequestParam Integer userId,
            @RequestParam Integer demoId) {
        
        Map<String, Object> response = new HashMap<>();
        
        try {
            List<VideoFinish> watchedVideos = videoFinishService.getWatchedVideos(userId, demoId);
            
            response.put("success", true);
            response.put("data", watchedVideos);
            response.put("message", "获取成功");
            
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "服务器错误: " + e.getMessage());
        }
        
        return response;
    }

    /**
     * 删除观看记录
     */
    @DeleteMapping("/delete-watched")
    public Map<String, Object> deleteVideoWatched(
            @RequestParam Integer userId,
            @RequestParam Integer demoId,
            @RequestParam Integer videoId) {
        
        Map<String, Object> response = new HashMap<>();
        
        try {
            boolean result = videoFinishService.deleteVideoWatched(userId, demoId, videoId);
            
            response.put("success", result);
            response.put("message", result ? "删除成功" : "删除失败");
            
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "服务器错误: " + e.getMessage());
        }
        
        return response;
    }
} 